import { useEffect } from 'react';
import { useAuthStore } from '@/store';
import { useAutoLogin } from '@/hooks/usePrivateRoute';
import { useRouter } from 'next/router';

export default function useRefreshToken() {
  const { token, refreshToken, tokenExpiration } = useAuthStore();
  const {
    isAutoLogin,
  } = useAutoLogin()

  useEffect(() => {
    if (isAutoLogin || !token) {
      return; // 如果路由中有 user 参数并且 store 中没有 token，则不刷新
    }
    const timer: NodeJS.Timer = setInterval(() => {
      if (tokenExpiration <= Date.now()) {
        refreshToken()
      }
    }, 1000);
    return () => clearInterval(timer);
  }, [refreshToken, token, isAutoLogin, tokenExpiration])
}

